﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;
using StairsModuleAndEdit.HYElevator.DataInfo;
using StairsModuleAndEdit.Properties;
using StairsModuleAndEdit.StairsModel;
using StairsModuleAndEdit.StairsModel.CommonUnit;

namespace StairsModuleAndEdit.HYElevator.ShowForm
{
	public partial class ElevSettingsFrm : System.Windows.Forms.Form
	{
		public ElevSettingsFrm(Document Doc)
		{
			WinDPIManager.GetWinFormXYDpi(ref this.m_dXScale, ref this.m_dYScale);
			this.InitializeComponent();
			//base.Icon = Resource.YArchitechIcon;
			this.m_Doc = Doc;
			this.m_Datapacher = XmlDataPacker.GetInstance();
			if (!this.ReadFileData())
			{
				return;
			}
			this.hyGroupBoxBlackSide1.Visible = false;
			this.hyGroupBoxBlackSide2.Visible = false;
			base.ClientSize = new Size((int)(219.0 * this.m_dXScale), (int)(480.0 * this.m_dYScale));
			this.btnSimpleOrmore.Text = "更多";
		}

		private bool ReadFileData()
		{
			string userDataLocation = Product.UserDataLocation;
			string path = userDataLocation + "\\PutElevator.dat";
			int selectedIndex = 0;
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				foreach (XmlConfigData xmlConfigData in this.m_Datapacher.m_xmlConfigData)
				{
					string elevatorType = MatchType.GetElevatorType(xmlConfigData.ElevatorType);
					this.hyCmbKind.AddItem(elevatorType);
				}
				this.hyCmbKind.SelectedIndex = selectedIndex;
			}
			else
			{
				FileStream fileStream = new FileStream(path, FileMode.Open);
				StreamReader streamReader = new StreamReader(fileStream);
				streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
				base.StartPosition = FormStartPosition.Manual;
				int x = Convert.ToInt32(streamReader.ReadLine());
				int y = Convert.ToInt32(streamReader.ReadLine());
				base.Location = new System.Drawing.Point(x, y);
				selectedIndex = Convert.ToInt32(streamReader.ReadLine());
				foreach (XmlConfigData xmlConfigData2 in this.m_Datapacher.m_xmlConfigData)
				{
					string elevatorType2 = MatchType.GetElevatorType(xmlConfigData2.ElevatorType);
					this.hyCmbKind.AddItem(elevatorType2);
				}
				this.hyCmbKind.SelectedIndex = selectedIndex;
				selectedIndex = Convert.ToInt32(streamReader.ReadLine());
				this.hycmbDoorKind.SelectedIndex = selectedIndex;
				selectedIndex = Convert.ToInt32(streamReader.ReadLine());
				this.hycmbBalPos.SelectedIndex = selectedIndex;
				streamReader.Close();
				fileStream.Close();
			}
			return true;
		}

		public bool SaveSettings()
		{
			string userDataLocation = Product.UserDataLocation;
			string path = userDataLocation + "\\PutElevator.dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			int x = base.Location.X;
			int y = base.Location.Y;
			streamWriter.WriteLine(x.ToString());
			streamWriter.WriteLine(y.ToString());
			streamWriter.WriteLine(this.hyCmbKind.SelectedIndex.ToString());
			streamWriter.WriteLine(this.hycmbDoorKind.SelectedIndex.ToString());
			streamWriter.WriteLine(this.hycmbBalPos.SelectedIndex.ToString());
			streamWriter.Close();
			fileStream.Close();
			return true;
		}

		private void btnSimpleOrmore_Click(object sender, EventArgs e)
		{
			if (this.btnSimpleOrmore.Text.Equals("更多"))
			{
				this.hyGroupBoxBlackSide1.Visible = true;
				this.hyGroupBoxBlackSide2.Visible = true;
				base.ClientSize = new Size((int)(219.0 * this.m_dXScale), (int)(667.0 * this.m_dYScale));
				this.btnSimpleOrmore.Text = "精简";
				return;
			}
			this.hyGroupBoxBlackSide1.Visible = false;
			this.hyGroupBoxBlackSide2.Visible = false;
			base.ClientSize = new Size((int)(219.0 * this.m_dXScale), (int)(480.0 * this.m_dYScale));
			this.btnSimpleOrmore.Text = "更多";
		}

		private void hyCmbKind_cmbSelectedChanged(object sender, EventArgs e)
		{
			string strKind = this.hyCmbKind.SelectedItem.ToString();
			this.m_Xmldata = this.m_Datapacher.m_xmlConfigData.Find((XmlConfigData val) => val.ElevatorType == MatchType.GetElevatorType(strKind));
			int num = this.FindMaxNumByElevator();
			string str = "DT";
			this.hyTxtNum.Text = str + (num + 1).ToString();
			this.hycmbDoorKind.Clear();
			foreach (DoorType selDoorType in this.m_Xmldata.LstDoorType)
			{
				this.hycmbDoorKind.AddItem(MatchType.GetDoorType(selDoorType));
			}
			this.hycmbDoorKind.SelectedIndex = 0;
			this.hyCmbRateWeight.Clear();
			foreach (KeyValuePair<int, SizeAndPersonNum> keyValuePair in this.m_Xmldata.DicLoadToSize)
			{
				this.hyCmbRateWeight.AddItem(keyValuePair.Key.ToString());
			}
			this.hyCmbRateWeight.SelectedIndex = 0;
			string s = this.hyCmbRateWeight.SelectedItem.ToString();
			int settingsByRatedWeight = 0;
			int.TryParse(s, out settingsByRatedWeight);
			this.SetSettingsByRatedWeight(settingsByRatedWeight);
			this.SetRateSpeedByElevatorKind();
			this.hycmbBalPos.Clear();
			this.hycmbBalPos.AddItem("后置");
			if (MatchType.GetElevatorType(strKind) != ElevatorType.ELEVATOR)
			{
				this.hycmbBalPos.AddItem("左置");
				this.hycmbBalPos.AddItem("右置");
			}
			this.hycmbBalPos.SelectedIndex = 0;
		}

		private void hyCmbRateWeight_cmbSelectedChanged(object sender, EventArgs e)
		{
			string s = this.hyCmbRateWeight.SelectedItem.ToString();
			int settingsByRatedWeight = 0;
			int.TryParse(s, out settingsByRatedWeight);
			this.SetSettingsByRatedWeight(settingsByRatedWeight);
			this.SetRateSpeedByElevatorKind();
		}

		private void SetSettingsByRatedWeight(int nRateWeight)
		{
			if (!this.m_Xmldata.DicLoadToSize.ContainsKey(nRateWeight))
			{
				return;
			}
			SizeAndPersonNum sizeAndPersonNum = this.m_Xmldata.DicLoadToSize[nRateWeight];
			this.hytxtDoorWidth.Text = sizeAndPersonNum.m_nDoorWidth.ToString();
			this.hytxtDoorHeight.Text = sizeAndPersonNum.m_nDoorHeight.ToString();
			this.chbAlarm.Checked = false;
			this.hytxtCarWidth.Text = sizeAndPersonNum.m_nCarWidth.ToString();
			this.hytxtCarDepth.Text = sizeAndPersonNum.m_nCarDepth.ToString();
			this.hytxtCarHeight.Text = sizeAndPersonNum.m_nCarHeight.ToString();
		}

		private void SetRateSpeedByElevatorKind()
		{
			this.hycmbRateSpeed.Clear();
			string strType = this.hyCmbKind.SelectedItem.ToString();
			ElevatorType elevatorType = MatchType.GetElevatorType(strType);
			if (elevatorType == ElevatorType.CARGOLIFT)
			{
				string s = this.hyCmbRateWeight.SelectedItem.ToString();
				int num = 0;
				int.TryParse(s, out num);
				if (num == 3000)
				{
					this.hycmbRateSpeed.AddItem("0.63");
				}
				else
				{
					this.hycmbRateSpeed.AddItem("1.00");
				}
			}
			else
			{
				foreach (double num2 in this.m_Xmldata.lstRatedSpeed)
				{
					double num3 = num2;
					this.hycmbRateSpeed.AddItem(num3.ToString("f2"));
				}
			}
			this.hycmbRateSpeed.SelectedIndex = 0;
		}

		private void hycmbBalPos_cmbSelectedChanged(object sender, EventArgs e)
		{
			this.SetBitmap();
		}

		public void RefreshMaxNum()
		{
			int num = this.FindMaxNumByElevator();
			string str = "DT";
			if (num < 10)
			{
				this.hyTxtNum.Text = str + "0" + (num + 1).ToString();
				return;
			}
			this.hyTxtNum.Text = str + (num + 1).ToString();
		}

		private int FindMaxNumByElevator()
		{
			KindToFamily instance = KindToFamily.GetInstance();
			Dictionary<FamilyKind, string>.ValueCollection values = instance.DicFamilyName.Values;
			int num = 0;
			ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_SpecialityEquipment);
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Doc);
			filteredElementCollector.WherePasses(elementCategoryFilter).WhereElementIsNotElementType();
			foreach (Element element in filteredElementCollector)
			{
				FamilySymbol familySymbol = this.m_Doc.GetElement(element.get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM).AsElementId()) as FamilySymbol;
				string familyName = familySymbol.FamilyName;
				foreach (string a in values)
				{
					if (a == familyName)
					{
						string text = element.GetParameter("电梯编号").AsString();
						if (text.Contains("DT"))
						{
							text = text.Substring(2);
							int num2 = 0;
							int.TryParse(text, out num2);
							if (num2 > num)
							{
								num = num2;
								break;
							}
							break;
						}
					}
				}
			}
			return num;
		}

		private void hycmbRateSpeed_KeyPress(object sender, KeyPressEventArgs e)
		{
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			this.SaveSettings();
			base.Close();
		}

		public BaseParam GetBaseParam()
		{
			BaseParam baseParam = new BaseParam();
			baseParam.m_ElevatorType = MatchType.GetElevatorType(this.hyCmbKind.Text);
			int.TryParse(this.hyCmbRateWeight.Text, out baseParam.m_RatedLoad);
			baseParam.m_strName = this.hyTxtNum.Text;
			return baseParam;
		}

		public DoorParam GetDoorParam()
		{
			DoorParam doorParam = new DoorParam();
			doorParam.m_DoorType = MatchType.GetDoorType(this.hycmbDoorKind.Text);
			int num = 0;
			int.TryParse(this.hytxtDoorHeight.Text, out num);
			doorParam.m_nDoorHeight = Common.MMToFeet((double)num);
			int.TryParse(this.hytxtDoorWidth.Text, out num);
			doorParam.m_nDoorWidth = Common.MMToFeet((double)num);
			return doorParam;
		}

		public LiftCarParam GetCarParam()
		{
			LiftCarParam liftCarParam = new LiftCarParam();
			int num = 0;
			int.TryParse(this.hytxtCarDepth.Text, out num);
			liftCarParam.m_nCarDepeth = Common.MMToFeet((double)num);
			int.TryParse(this.hytxtCarHeight.Text, out num);
			liftCarParam.m_nCarHeight = Common.MMToFeet((double)num);
			int.TryParse(this.hytxtCarWidth.Text, out num);
			liftCarParam.m_nCarWidth = Common.MMToFeet((double)num);
			return liftCarParam;
		}

		public OtherParam GetOtherParam()
		{
			OtherParam otherParam = new OtherParam();
			double.TryParse(this.hycmbRateSpeed.Text, out otherParam.m_dRatedSpeed);
			otherParam.m_BalanceWeightPos = MatchType.GetBPos(this.hycmbBalPos.Text);
			return otherParam;
		}

		public bool GetAlarmInfo()
		{
			return this.chbAlarm.Checked;
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void ElevSettingsFrm_MouseLeave(object sender, EventArgs e)
		{
			if (!this.CheckInputValue())
			{
				YJKMessageBox.Show("对话框上存在空值情况，请检查!", MessageBoxButtons.OK);
			}
			int wMsg = 7;
			ElevSettingsFrm.SendMessage(CmdPutElevator._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		private bool CheckInputValue()
		{
			return !this.hytxtDoorWidth.Text.IsEmpty<char>() && !this.hytxtDoorHeight.Text.IsEmpty<char>() && !this.hytxtCarWidth.Text.IsEmpty<char>() && !this.hytxtCarDepth.Text.IsEmpty<char>() && !this.hytxtCarHeight.Text.IsEmpty<char>() && !this.hycmbRateSpeed.Text.IsEmpty<char>();
		}

		private void hycmbDoorKind_cmbSelectedChanged(object sender, EventArgs e)
		{
			this.SetBitmap();
		}

		private void SetBitmap()
		{
			DoorType doorType = MatchType.GetDoorType(this.hycmbDoorKind.SelectedItem.ToString());
			if (this.hycmbBalPos.ItemsCount == 0)
			{
				return;
			}
			BlanceWeightPos bpos = MatchType.GetBPos(this.hycmbBalPos.SelectedItem.ToString());
			if (doorType == DoorType.MIDTYPE && bpos == BlanceWeightPos.BALBEHIND)
			{
				this.picElevator.Image = StairsModuleAndEdit.Properties.Resources.中分门平衡块后置;
			}
			if (doorType == DoorType.MIDTYPE && BlanceWeightPos.BALLEFT == bpos)
			{
				this.picElevator.Image = StairsModuleAndEdit.Properties.Resources.中分门平衡块左置;
			}
			if (doorType == DoorType.MIDTYPE && BlanceWeightPos.BALRIGHT == bpos)
			{
				this.picElevator.Image = StairsModuleAndEdit.Properties.Resources.中分门平衡块右置;
			}
			if (DoorType.SIDETYPE == doorType && bpos == BlanceWeightPos.BALBEHIND)
			{
				this.picElevator.Image = StairsModuleAndEdit.Properties.Resources.旁开门平衡块后置;
			}
			if (DoorType.SIDETYPE == doorType && BlanceWeightPos.BALLEFT == bpos)
			{
				this.picElevator.Image = StairsModuleAndEdit.Properties.Resources.旁开门平衡块左置;
			}
			if (DoorType.SIDETYPE == doorType && BlanceWeightPos.BALRIGHT == bpos)
			{
				this.picElevator.Image = StairsModuleAndEdit.Properties.Resources.旁开门平衡块右置;
			}
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void ElevSettingsFrm_FormClosed(object sender, FormClosedEventArgs e)
		{
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			ElevSettingsFrm.keybd_event(bVk, 0, dwFlags, 0);
			ElevSettingsFrm.keybd_event(bVk, 0, dwFlags2, 0);
		}

		private XmlDataPacker m_Datapacher;

		private XmlConfigData m_Xmldata;

		private Document m_Doc;

		private double m_dXScale = 1.0;

		private double m_dYScale = 1.0;
	}
}
